package com.ctshk.rpc.order.airexpress.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.OrderCollectionStatus;
import com.ctshk.common.enums.SystemBusinessType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.rpc.finance.req.bill.BillOperateRefundReq;
import com.ctshk.rpc.finance.service.IFinanceBillService;
import com.ctshk.rpc.order.airexpress.entity.AirportExpressOrderCollection;
import com.ctshk.rpc.order.airexpress.mapper.AirportExpressOrderCollectionMapper;
import com.ctshk.rpc.order.airexpress.service.IAirportExpressOrderCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 机场快线收款明细 服务实现类
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-23
 */
@Slf4j
@DubboService
public class AirportExpressOrderCollectionServiceImpl extends ServiceImpl<AirportExpressOrderCollectionMapper, AirportExpressOrderCollection> implements IAirportExpressOrderCollectionService {
    @Autowired
    AirportExpressOrderCollectionMapper airportExpressOrderCollectionMapper;
    @DubboReference
    IFinanceBillService financeBillService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int reconfirm(Long id, Long userId) {
        AirportExpressOrderCollection airportExpressOrderCollection = airportExpressOrderCollectionMapper.selectById(id);
        if (airportExpressOrderCollection == null) {
            //收款记录不存在
            throw new BusinessException(SystemError.AIREXPRESS_ORDER_60014);
        }
        if (!OrderCollectionStatus.REJECT.getCode().equals(airportExpressOrderCollection.getStatus())) {
            // 收款记录的状态必须是驳回，才能再次确认
            throw new BusinessException(SystemError.AIREXPRESS_ORDER_60015);
        }
        airportExpressOrderCollection.setStatus(null);
        airportExpressOrderCollection.setRejectInfo("");
        airportExpressOrderCollection.setModifiedId(userId);
        airportExpressOrderCollection.setGmtModified(LocalDateTime.now());
        int num = airportExpressOrderCollectionMapper.updateById(airportExpressOrderCollection);
        /**
         * 二次确认，同步状态到财务
         */
        BillOperateRefundReq billOperateRefundReq = new BillOperateRefundReq();
        billOperateRefundReq.setBillNumber(airportExpressOrderCollection.getCollectionNumber());
        billOperateRefundReq.setBusinessType(SystemBusinessType.AIR_EXPRESS.getCode());
        billOperateRefundReq.setUserId(userId);
        log.info("【机场快线订单收款】二次确认财务收款记录，req:{}", JSON.toJSONString(billOperateRefundReq));
        financeBillService.reConfirm(billOperateRefundReq);
        return num;
    }

    @Override
    public Result rejectPayment(String collectionNumber, Long userId, String msg) {
        log.info("【机场快线订单收款-财务驳回收款】方法入参,number:{},msg:{},userId:{}", collectionNumber, msg, userId);
        AirportExpressOrderCollection airportExpressOrderCollection = airportExpressOrderCollectionMapper.selectOne(Wrappers.<AirportExpressOrderCollection>lambdaQuery()
                .eq(AirportExpressOrderCollection::getCollectionNumber, collectionNumber)
                .eq(AirportExpressOrderCollection::getIsDeleted, IsDeletedCode.NO.getCode())
                .select(AirportExpressOrderCollection::getId, AirportExpressOrderCollection::getStatus));
        if (airportExpressOrderCollection == null) {
            //收款记录不存在
            return Result.failed(SystemError.AIREXPRESS_ORDER_60014);
        }
        if (Objects.equals(airportExpressOrderCollection.getStatus(), OrderCollectionStatus.REJECT.getCode())) {
            // 收款记录的状态必须不等于“已驳回”
            return Result.failed(SystemError.AIREXPRESS_ORDER_60015);
        }
        airportExpressOrderCollection.setStatus(OrderCollectionStatus.REJECT.getCode());
        airportExpressOrderCollection.setRejectInfo(msg);
        airportExpressOrderCollection.setModifiedId(userId);
        airportExpressOrderCollection.setGmtModified(LocalDateTime.now());
        airportExpressOrderCollectionMapper.update(null, new UpdateWrapper<AirportExpressOrderCollection>().lambda()
                .eq(AirportExpressOrderCollection::getId, airportExpressOrderCollection.getId())
                .set(AirportExpressOrderCollection::getStatus, OrderCollectionStatus.REJECT.getCode())
                .set(AirportExpressOrderCollection::getRejectInfo, msg)
                .set(AirportExpressOrderCollection::getModifiedId, userId)
                .set(AirportExpressOrderCollection::getGmtModified, LocalDateTime.now()));
        return Result.success();
    }
}
